package com.example.controller;


import com.example.config.ErrorCodeEnum;
import com.example.pojo.Users;
import com.example.service.UserService;
import com.example.util.RedisUtil;
import com.power.common.model.CommonResult;
import com.power.common.util.AESUtil;
import com.power.common.util.DateTimeUtil;
import com.power.common.util.RSAUtil;
import com.power.common.util.StringUtil;
import io.swagger.annotations.*;
import io.swagger.models.HttpMethod;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;


import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.sql.Timestamp;
import java.util.Map;


@Controller
@RequestMapping("/service")
@Api(tags = "用户数据接口")
public class HelloController extends BaseController {

    @Autowired
    private UserService userService;
    @Autowired
    private RedisUtil redisUtil;

    @ApiOperation(value = "增加用户")
    @ApiImplicitParam(name = "users",value = "用户信息",required = true)
    @RequestMapping(value = "/insert",method = {RequestMethod.POST})
    public  void  insert(@RequestBody Users users ,HttpServletRequest request,HttpServletResponse response){
        //时间
//        long now = DateTimeUtil.getNowTime();
//        System.out.println("当前时间戳>>>"+now);
//        Timestamp timestamp = DateTimeUtil.nowTimeStamp();
//        System.out.println("当前时间Timestamp>>>" + timestamp);
//        String nowstr = DateTimeUtil.nowStrTime(DateTimeUtil.DATE_FORMAT_CHINESE_SECONDE);
//        System.out.println("当前时间字符串>>>>" + nowstr);
        //字符串
         ///空字符串判断
        if (StringUtil.isEmpty("null")){
            System.out.println("空字符串");
        }
         ///不为空字符串判断
        if (StringUtil.isNotEmpty("null")){

        }else{
            System.out.println("空字符串");
        }
         ///去掉空格 trim(): 去掉全部空格 trimBlank:只去掉首尾空格
        String str = StringUtil.trim(" h h h ");
        System.out.println("trim==>" + str);
        String oneStr = StringUtil.trimBlank(" h h h ");
        System.out.println(oneStr);

        System.out.println("AES加密解密工具类");
        String string = "欢迎访问 hangge.com"; //原始字符串
        String key = "aespower98765432";// 密钥
        String encprypted = AESUtil.encodeByECB(string,key); //开始加密
        System.out.println("加密后的字符串" + encprypted);
        String decrypted = AESUtil.decodeByECB(encprypted,key);
        System.out.println("解密后的字符串"+decrypted);

        //RSA 加密解密工具类
        Map<String,String> keyMap = RSAUtil.createKeys(2048);
        String privateKEy = keyMap.get(RSAUtil.PRIVATE_KEY);
        System.out.println("私钥：" + privateKEy);
        String publicKey  = keyMap.get(RSAUtil.PUBLIC_KEY);
        System.out.println("公钥：" + publicKey);
        //使用公钥来加密字符串
        String encodeData = RSAUtil.encryptString(string,publicKey);
        System.out.println("密文:" +encodeData);
        //使用私钥解密已加密的字符串
        String decodeData = RSAUtil.decryptString(encodeData,privateKEy);
        System.out.println("解密后文字：" + decodeData);

        this.writeJson(CommonResult.ok(ErrorCodeEnum.SUCCESS),response);
    }



    @ApiOperation(value = "查询用户",notes = "根据id查询用户")
    @ApiImplicitParam(paramType = "path",name = "id",value = "用户id",required = true)
    @RequestMapping(value = "/seletById",method = {RequestMethod.GET})
    public void seletById(Integer id,HttpServletRequest request, HttpServletResponse response) {
        Users users = userService.seletById(id);
        this.writeJson(users, response);
    }

    @RequestMapping(value = "/testRedisObject",method = {RequestMethod.GET})
    public void testRedisObject(HttpServletRequest request,HttpServletResponse response){
         System.out.println(redisUtil.get("2"));
         this.writeJson("删除", response);
    }





}